package com.ochnas.volunteersignup.ui;

import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Pattern;

import javax.persistence.NoResultException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;

public class LoginFormHandler {
	public static ApplicationContext spring = 
		new ClassPathXmlApplicationContext("applicationContext.xml");
	private UserService userService = (UserService) spring.getBean("userService");
	private Map<String, String> errors = new Hashtable<String, String>();
	private static Pattern emailAddressPattern = Pattern.compile("[^\\s@]+[@{1}+][^\\s@]+");	
	private static String emailAddressProperty = "emailAddress";
	private static String passwordProperty = "password";
	private String emailAddress = "";
	private String password = "";
	
	public LoginFormHandler() {
		super();
	}
	
	public boolean validate() {
		errors.clear();
		
		boolean isValid = true;
		if (emailAddress == null || (emailAddress != null && emailAddress.equals(""))) {
			emailAddress = "";
			errors.put(emailAddressProperty, "Please enter email address.");
			isValid = false;
		} else if (!emailAddressPattern.matcher(emailAddress).matches()) {
			errors.put(emailAddressProperty, "Please enter a valid email address.");
			isValid = false;
		} else {
			if (errors.containsKey(emailAddressProperty)) errors.remove(emailAddressProperty);
		}
		
		if (password == null || (password != null && password.equals(""))) {
			password = "";
			errors.put(passwordProperty, "Please enter password.");
			isValid = false;
		} else {
			if (errors.containsKey(passwordProperty)) errors.remove(passwordProperty);
		}
		
		// if any error occurs with the email address, erase the password
		if (errors.containsKey(emailAddressProperty)) {
			password = "";
			if (errors.containsKey(passwordProperty)) errors.remove(passwordProperty);
		}
		
		return isValid;
	}
	
	
	public String getErrorMsg(String key) {
		String value = errors.get(key.trim());
		return (value==null?"":value);
	}
	
	public void setErrorMsg(String key, String value) {
		errors.put(key, value);
	}
	
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public User login() {
		User u = null;
		try {
			u = userService.loginUserAccount(emailAddress, password);
		} catch (NoResultException e) {
			password = "";
			errors.put("errMsg", e.getMessage());
		}
		return u;
	}

	public static void main(String[] args) {
		String e = "jeanda.r.hoang@hotmail.us.net";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "je@anda@hotmail.com";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda@hotmail.com";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda.r.hoang@hotmail.com";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda.r.hoang@hotmail";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda.r.hoang@";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda.r.hoang";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
		e = "jeanda";
		System.out.println(e+" isValid ? " + emailAddressPattern.matcher(e).matches());
	}

}
